package com.cjack.frenchexcel.util;

import cn.hutool.core.bean.BeanUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 将db对应的table实体转化为供前端页面使用的model对象
 * Created by root on 6/23/19.
 */
public final class ModelUtils {

    public static <T> T copySignModel(Object source, Class<T> destClass) {
        T dest = null;
        try {
            dest = destClass.newInstance();
            BeanUtil.copyProperties(source, dest);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
        return dest;
    }

    public static void copySignModel(Object source, Object dest) {
        BeanUtil.copyProperties(source, dest);
    }

    public static <T> List<T> copyListModel(List sources, Class<T> destClass) {
        List<T> dests = new ArrayList<>(sources.size());
        try {
            for (Object source : sources) {
                T dest = destClass.newInstance();
                BeanUtil.copyProperties(source, dest);
                dests.add(dest);
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
        return dests;
    }
}
